#include "hsi_lookup.h"

void rgb2hsi(LPWORD rgb565, LPWORD hImg, LPBYTE sImg, LPBYTE iImg, int width, int height) {
  int r, c;
  int index;
  int tempRgb;

  for (r = 0; r < height; r++) {
    index = r * width;

    for (c = 0; c < width; c++) {
      tempRgb = rgb565[index + c];

      hImg[index + c] = h_lookup[tempRgb];
      sImg[index + c] = s_lookup[tempRgb];
      iImg[index + c] = i_lookup[tempRgb];
    }
  }
}

/*
 * input : rgb888
 * output : rgb565
 */
void rgb888to565(LPBYTE rgb888, LPWORD rgb565, int width, int height) {
  int x, y, index, index2;

  for (y = 0; y < height; y++) {
    index = y * width;
    for (x = 0; x < width; x++) {
      index2 = index + x;
      rgb565[index2] = MAKE_RGB565(rgb888[index2 * 3 + 2], rgb888[index2 * 3 + 1],
                                   rgb888[index2 * 3]);
    }
  }
}

/*
 * input: rgb888
 * output: hsi
 */
void rgb888toHsi(LPBYTE rgb888, LPWORD hImg, LPBYTE sImg, LPBYTE iImg, int width, int height) {
	int x, y;
	int index;
	int rgb565;

	for (y = 0; y < height; y++) {
		index = y * width;

		for (x = 0; x < width; x++) {
			rgb565 = MAKE_RGB565(rgb888[(index + x) * 3 + 2], rgb888[(index + x) * 3 + 1], rgb888[(index + x) * 3]);
      hImg[index + x] = h_lookup[rgb565];
      sImg[index + x] = s_lookup[rgb565];
      iImg[index + x] = i_lookup[rgb565];
		}
	}
}

